export default class TimerCall {
    public callTime: string;
    private durtime: number;
    private callTimeInter: any;

    constructor() {
        this.callTime = "00:00:00";
        this.durtime = 0;
        this.callTimeInter = null;
    }

    //启动计时器
    startTimerCall(callback: (value: string) => void) {
        if (this.callTimeInter) {
            clearInterval(this.callTimeInter);
            this.callTime = "00:00:00";
            this.durtime = 0;
            callback(this.callTime)
        }
        this.callTimeInter = setInterval(() => {
            this.timerCall();
            callback(this.callTime)
        }, 1000)
    }

    //停止计时器
    stopTimerCall(callback: (value: string) => void) {
        if (this.callTimeInter) {
            clearInterval(this.callTimeInter);
            this.callTime = "00:00:00";
            this.durtime = 0;
        }
        callback(this.callTime)
    }

    //计时器
    timerCall() {
        this.durtime++;
        let sec: string | number = this.durtime % 60;
        if (sec < 10) {
            sec = "0" + sec;
        }
        let min: string | number = Math.floor((this.durtime % (60 * 60)) / 60);
        if (min < 10) {
            min = "0" + min;
        }
        let hour: string | number = Math.floor(this.durtime / (60 * 60));
        if (hour < 10) {
            hour = "0" + hour;
        }
        this.callTime = hour + ":" + min + ":" + sec
    }

}